import { DetailsPage } from './../../pages/details/details';
import { Component, Input } from '@angular/core';
import { BaseUI } from '../../common/baseui';
import { RestProvider } from '../../providers/rest/rest';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { NavParams } from 'ionic-angular/navigation/nav-params';
import { NavController } from 'ionic-angular/navigation/nav-controller';
import { Storage } from '@ionic/storage';

/**
 * Generated class for the QuestionListComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'question-list',
  templateUrl: 'question-list.html'
})
export class QuestionListComponent extends BaseUI {

  errorMessage: any
  questions: string[]

  //datatype 外部传入的参数
  @Input ('datatype') dataSourceType

  constructor(public navCtrl: NavController,
    public storage: Storage,
    public rest: RestProvider,
    public loadCtrl:LoadingController,
    public navParams: NavParams) {
   super()
  }

  //组件没有ionc生命周期的定义 只有angular的生命周期
  ngAfterContentInit() {
    this.storage.get('userId').then((val)=> {
      if(val!=null) {
        let loading = super.showLoading(this.loadCtrl, "加载中...")
        this.rest.getUserQuestionList(val, this.dataSourceType)
        .subscribe(
          q => {
           this.questions = q
           //View不能隐藏的错误
            loading.dismissAll()
          },
         error=> this.errorMessage = <any>error
        )
      }
    })
  }

  gotoDetails(questionId) {
    this.navCtrl.push(DetailsPage, {id: questionId})
  }
}
